package com.todod.common.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TododDate extends Date {

	private static final long serialVersionUID = -3800601650830775341L;
	private static TododDate instance;
	Date today;

	public TododDate() {
		today = new Date();

	}

	public static TododDate getInstance() {
		instance = new TododDate();

		return instance;
	}

	public String getCurrentMinute() {

		SimpleDateFormat formatdatetime = new SimpleDateFormat("HH:mm");
		return formatdatetime.format(today);

	}

	public long getTimeStamp() {

		return today.getTime();

	}

	// get the difference of two dates.得到两个日期的日期差
	public long getDiffDay(String firstDate, String secondDate) {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		long day = 0;
		try {
			java.util.Date date = myFormatter.parse(firstDate);
			java.util.Date mydate = myFormatter.parse(secondDate);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return day;

	}

	// get the difference of two dates.得到两个日期的日期差结果单位为年（余小数一位）
	public static String getDiffYear(String firstDate, String secondDate) {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		String resultYear = "";
		long day = 0;
		try {
			java.util.Date date = myFormatter.parse(firstDate);
			java.util.Date mydate = myFormatter.parse(secondDate);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);

			BigDecimal b1 = new BigDecimal(day);
			BigDecimal b2 = new BigDecimal(365);

			BigDecimal year1 = b1.divide(b2, 3, BigDecimal.ROUND_HALF_EVEN);
			// year1.setScale(1, BigDecimal.ROUND_DOWN);
			// resultYear = year1.toString();
			resultYear = year1.toString();
			resultYear = resultYear.substring(0, resultYear.indexOf("."));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resultYear;

	}

	// get the difference of two dates.得到两个日期的日期差结果单位为月（余小数一位）
	public long getDiffMonth(String firstDate, String secondDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		int resultMonth = 0;
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		c1.setTime(sdf.parse(firstDate));
		c2.setTime(sdf.parse(secondDate));

		int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);

		// 开始日期若小于结束日期
		if (year < 0) {
			year = -year;
			resultMonth = year * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
		} else {
			resultMonth = year * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
		}

		return resultMonth;
	}

	// SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
	// String resultMonth = "";
	// long day = 0;
	// try {
	// java.util.Date date = myFormatter.parse(firstDate);
	// java.util.Date mydate = myFormatter.parse(secondDate);
	// day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
	//
	// BigDecimal b1 = new BigDecimal(day);
	// BigDecimal b2 = new BigDecimal(360);
	//
	// BigDecimal year1 = b1.divide(b2, 1, BigDecimal.ROUND_HALF_EVEN);
	//
	// resultMonth = year1.toString();
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }

	// return resultMonth;
	//
	// }

	/**
	 * @return String:for example:"2004-11-02??
	 */
	public String getSimpledate() {
		SimpleDateFormat formatdate = new SimpleDateFormat("yyyy-MM-dd");
		return formatdate.format(today);

	}

	/**
	 * @return String:for example:"2004-11-02??
	 */
	public String getdateTim() {
		SimpleDateFormat formatdate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return formatdate.format(today);

	}

	/**
	 * @return String :for example:2008-11-02 09:24:45
	 */
	public String getDateTime() {

		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
		return formatdatetime.format(today);

	}

	/**
	 * 得到n天前的日期时间
	 * 
	 * @return String :for example:2008-11-02 09:24:45
	 */
	public String getLastdayDateTime(int nday) {
		Date ndaydate = new Date(System.currentTimeMillis() - nday * 24 * 60 * 60 * 1000);

		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
		return formatdatetime.format(ndaydate);

	}

	/**
	 * 得到n天前的日期
	 * 
	 * @return String :for example:2008-11-02 09:24:45
	 */
	public String getLastdayDate(int nday) {
		Date ndaydate = new Date(System.currentTimeMillis() - nday * 24 * 60 * 60 * 1000);

		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd");
		return formatdatetime.format(ndaydate);

	}

	/*
	 * 获取输入日期前三天 年-月
	 */
	public String getLastYearMonth(String beginTime) {
		String endTime = "";
		try {
			SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");

			Date beginDate = df1.parse(beginTime);

			Calendar calendar = Calendar.getInstance();

			calendar.setTime(beginDate);
			calendar.add(Calendar.DATE, -3);
			beginDate = calendar.getTime();
			SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM");
			endTime = formatdatetime.format(beginDate);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return endTime;

	}

	/**
	 * 得到n天前的日期
	 * 
	 * @return String :for example:2008-11-02 09:24:45
	 */
	public String getLastdayDate2(int nday) {
		Date ndaydate = new Date(System.currentTimeMillis() - nday * 24 * 60 * 60 * 1000);

		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyyMMdd");
		return formatdatetime.format(ndaydate);

	}

	/**
	 * 
	 * @return String : for example: "20080725"
	 */
	public String getYmd() {

		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyyMMdd");

		return formatdatetime.format(today);

	}

	/**
	 * 
	 * @return onle -- year
	 */
	public int getDateYear() {
		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy");

		return Integer.parseInt(formatdatetime.format(today));

	}

	public String getDateMonth() {
		SimpleDateFormat formatdatetime = new SimpleDateFormat("MM");

		return formatdatetime.format(today);

	}

	public String getDateMonth2() {

		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		// String time = format.format(c.getTime());

		SimpleDateFormat formatdatetime = new SimpleDateFormat("MM");
		String aa = formatdatetime.format(c.getTime());

		if (aa.compareTo("10") < 0) {
			return aa.substring(1);
		}

		return aa;

	}

	public String getDateDay() {
		SimpleDateFormat formatdatetime = new SimpleDateFormat("dd");

		return formatdatetime.format(today);

	}

	private final int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG,
			DateFormat.FULL };
	private static Locale locale = new Locale("zh", "TW");

	public String getCurrentDate() {
		Date current = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(styles[4], styles[3], locale);
		String currentDate = dateFormat.format(current);

		return currentDate;
	}

	public String getCurrentTime() {
		Date current = new Date();
		DateFormat dateFormat = DateFormat.getDateInstance(styles[3], locale);
		String currentDate = dateFormat.format(current);

		return currentDate;
	}

	public String getPeriod() {
		SimpleDateFormat formatdate = new SimpleDateFormat("yyyy-MM");
		return formatdate.format(today);

	}

	public String getSimplePeriod() {
		SimpleDateFormat formatdate = new SimpleDateFormat("yyyyMM");
		return formatdate.format(today);

	}

	public String getPeriod2() {
		SimpleDateFormat formatdate = new SimpleDateFormat("yyyyMM");
		return formatdate.format(today);

	}

	public String getPeriod3() {
		SimpleDateFormat formatdate = new SimpleDateFormat("yyMM");
		return formatdate.format(today);

	}

	public String getSimpledate2() {
		SimpleDateFormat formatdate = new SimpleDateFormat("yyMMdd");
		return formatdate.format(today);

	}

	public String getSimpledate3() {
		SimpleDateFormat formatdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatdate.format(today);

	}

	/**
	 * 获得两个日期的差值（秒）
	 * 
	 * @return
	 */
	public long getDifferentTime(String beginDateTime, String endDateTime) {
		long differentTime = 0;
		try {
			// String begin_date = "2013-01-23 13:34:35";
			// String end_date = "2013-01-23 13:35:39";
			SimpleDateFormat df1 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
			SimpleDateFormat df2 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
			Date d1 = df1.parse(beginDateTime);
			Date d2 = df2.parse(endDateTime);
			long time1 = d1.getTime();
			long time2 = d2.getTime();
			differentTime = (time1 - time2) / 1000;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return differentTime;

	}

	/**
	 * 得到后几天日期
	 * 
	 * @param nday
	 * @return
	 */
	public String getNextDay(int nday) {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, nday);
		date = calendar.getTime();
		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd");
		return formatdatetime.format(date);

	}

	/**
	 * 得到后几天日期时间
	 * 
	 * @param nday
	 * @return
	 */
	public static String getNextDayTime(int nday) {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, nday);
		date = calendar.getTime();
		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatdatetime.format(date);

	}

	/**
	 * 得到后几天日期时间
	 * 
	 * @param nday
	 * @return
	 */
	public String getNextDayTime(String beginTime, int nday) {
		String endTime = "";
		try {
			SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

			Date beginDate = df1.parse(beginTime);

			Calendar calendar = Calendar.getInstance();

			calendar.setTime(beginDate);
			calendar.add(Calendar.DAY_OF_MONTH, nday);
			beginDate = calendar.getTime();
			SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			endTime = formatdatetime.format(beginDate);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return endTime;

	}

	/**
	 * 得到后几天日期时间
	 * 
	 * @param nday
	 * @return
	 */
	public String getNextDayDate(String beginTime, int nday) {
		String endTime = "";
		try {
			SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");

			Date beginDate = df1.parse(beginTime);

			Calendar calendar = Calendar.getInstance();

			calendar.setTime(beginDate);
			calendar.add(Calendar.DAY_OF_MONTH, nday);
			beginDate = calendar.getTime();
			SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd");
			endTime = formatdatetime.format(beginDate);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return endTime;

	}

	/**
	 * @return String :for example:2008年11月02日09时24分45秒
	 */
	public static String getDateTimeShow(String date) {
		String endTime = "";
		try {
			SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			Date beginDate = df1.parse(date);
			SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
			endTime = formatdatetime.format(beginDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return endTime;

	}

	public static String getFromOtherDate(String date) {
		String endDate = "";
		try {
			SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd");

			Date beginDate = df1.parse(date);
			SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd");
			endDate = formatdatetime.format(beginDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return endDate;

	}

	/**
	 * 把TimeStamp转成String
	 * 
	 * @param ts
	 * @return
	 */
	public String transTimeStampToString(Timestamp ts) {

		// Timestamp ts = new Timestamp(System.currentTimeMillis());
		String tsStr = "";
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {

			tsStr = sdf.format(ts);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return tsStr;
	}

	/**
	 * 得到以后几分钟的时间
	 * 
	 * @return String :for example:2008-11-02 09:24:45
	 */
	public String getLastMinuteDate(int nminute) {
		Date ndaydate = new Date(System.currentTimeMillis() - nminute * 60 * 1000);

		SimpleDateFormat formatdatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatdatetime.format(ndaydate);

	}

	/**
	 * 
	 * @Title: getDiffDayByNewDate
	 * @Description: 比较传入时间与当前时间的差（yyyy-MM-dd）
	 * @author：gsh
	 * @date: 2018-5-18
	 * @param date
	 * @return
	 * @return long 大于当前时间：负，小于当前时间 ：正
	 */
	public static long getDiffByNewDate(String secondDate) {

		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		long day = 0;
		try {
			java.util.Date date = myFormatter.parse(myFormatter.format(new Date()));
			java.util.Date mydate = myFormatter.parse(secondDate);
			day = (date.getTime() - mydate.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return day;

	}

	public static void main(String[] args) {

		System.out.println(getNextDayTime(-2));
	}

	/**
	 * @return Fri Feb 02 18:18:49 CST 2018 转 yyyy-MM-dd HH:mm:ss
	 */
	public static String changeDateTime(Date date) {
		String changedate = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			changedate = df.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return changedate;

	}

	/**
	 * 把TimeStamp转成String
	 * 
	 * @param ts
	 * @return
	 */
	public String timeStampToString(Long ts) {

		String sd = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 时间格式
		try {
			sd = sdf.format(new Date(ts)); // 时间戳转换成时间
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sd;
	}
}
